﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data;

namespace PayRoller.Manage.QuickAdd
{
    public class Payments : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button btnEdit;
        protected DropDownList dlEmp;
        //protected DropDownList dlOrg;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        protected TextBox txtOrgName;
        //protected TextBox txtEmpName;
        protected Label labOrgName;
        protected Label labEmpName;
       

        // Methods
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtOrgName.Text) == false)
            {
                if (txtOrgName.Text.Contains("[") && txtOrgName.Text.Contains("]"))
                {
                    int beginPos = txtOrgName.Text.LastIndexOf("[")+1;
                    int endPos = txtOrgName.Text.LastIndexOf("]");
                    string orgID = txtOrgName.Text.Substring(beginPos,endPos-beginPos);

                    if (dlEmp.Visible == false)
                    {
                        dlEmp.Visible = true;
                        labEmpName.Visible = true;

                        txtOrgName.Visible = false;
                        labOrgName.Visible = false;
                        this.GetEmpList(Convert.ToInt32(orgID));
                    }
                    else
                    {
                        base.Response.Redirect("EmpPay.aspx?OrgSid=" + orgID + "&EmpSid=" + this.dlEmp.SelectedValue);
                    }
                }
            }
            else
            { 
                
            }
            
            
        }

        //protected void dlOrg_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (this.dlOrg.SelectedValue != "-1")
        //    {
        //        this.GetEmpList(Convert.ToInt32(this.dlOrg.SelectedValue));
        //    }
        //    else
        //    {
        //        this.GetEmpList();
        //    }
        //}

        //public void GetEmpList()
        //{
        //    this.dlEmp.Items.Clear();
        //    TempinfoBN obn = new TempinfoBN(this.Page);
        //    DataTable list = obn.GetList();
        //    obn.Dispose();
        //    obn = null;
        //    ListItem item = new ListItem("Please select employee", "-1");
        //    this.dlEmp.Items.Add(item);
        //    for (int i = 0; i < list.Rows.Count; i++)
        //    {
        //        item = new ListItem(list.Rows[i]["EmpName"].ToString(), list.Rows[i]["Sid"].ToString());
        //        this.dlEmp.Items.Add(item);
        //    }
        //}

        public void GetEmpList(int OrgSid)
        {
            this.dlEmp.Items.Clear();
            TempinfoBN obn = new TempinfoBN(this.Page);
            obn.QueryOrgSid(OrgSid);
            /*不显示被删除的用户*/
            obn.Filter = string.Format("IsValid!={0}", (int)EmpStatuses.Deleted);

            DataTable list = obn.GetList();
            obn.Dispose();
            obn = null;
            ListItem item = new ListItem("Please select employee", "-1");
            this.dlEmp.Items.Add(item);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                item = new ListItem(list.Rows[i]["EmpName"].ToString(), list.Rows[i]["Sid"].ToString());
                this.dlEmp.Items.Add(item);
            }
        }

        //public void GetOrgList()
        //{
        //    TorginfoBN obn = new TorginfoBN(this.Page);
        //    DataTable list = obn.GetList();
        //    obn.Dispose();
        //    obn = null;
        //    ListItem item = new ListItem("Please select client", "-1");
        //    this.dlOrg.Items.Add(item);
        //    for (int i = 0; i < list.Rows.Count; i++)
        //    {
        //        item = new ListItem(list.Rows[i]["OrgName"].ToString(), list.Rows[i]["Sid"].ToString());
        //        this.dlOrg.Items.Add(item);
        //    }
        //}

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "0006";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //this.GetOrgList();
                //this.GetEmpList();
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
